package org.jeecg.common.util;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
public class TreeBuilder {
    List<Node> nodes = new ArrayList<>();

    public String buildTree(List<Node> nodes) {
        TreeBuilder treeBuilder = new TreeBuilder(nodes);
        return treeBuilder.buildJSONTree();
    }

    public TreeBuilder() {}

    public TreeBuilder(List<Node> nodes) {
        super();
        this.nodes = nodes;
    }

    // 构建JSON树形结构
    public String buildJSONTree() {
        List<Node> nodeTree = buildTree();
        String jsonStr = JSON.toJSONString(nodeTree);
        return jsonStr;
    }

    // 构建树形结构
    public List<Node> buildTree() {
        List<Node> treeNodes = new ArrayList<>();
        List<Node> rootNodes = getRootNodes();
        for (Node rootNode : rootNodes) {
            buildChildNodes(rootNode);
            System.out.println(rootNodes);
            treeNodes.add(rootNode);
        }
        return treeNodes;
    }

    // 递归子节点
    public void buildChildNodes(Node node) {
        List<Node> children = getChildNodes(node);
        if (!children.isEmpty()) {
            for (Node child : children) {
                buildChildNodes(child);
            }
            node.setChildren(children);
        }
    }

    // 获取父节点下所有的子节点
    public List<Node> getChildNodes(Node pnode) {
        List<Node> childNodes = new ArrayList<>();
        for (Node n : nodes) {
            if (pnode.getId().equals(n.getParentId())) {
                childNodes.add(n);
            }
        }
        return childNodes;
    }

    // 判断是否为根节点
    public boolean rootNode(Node node) {
        boolean isRootNode = true;
        for (Node n : nodes) {
            if (node.getParentId().equals(n.getId())) {
                isRootNode = false;
                break;
            }
        }
        return isRootNode;
    }

    // 获取集合中所有的根节点
    public List<Node> getRootNodes() {
        List<Node> rootNodes = new ArrayList<>();
        for (Node n : nodes) {
            if (rootNode(n)) {
                rootNodes.add(n);
            }
        }
        return rootNodes;
    }

    public static class Node {
        private String id;
        private String parentId;
        private String title;
        private String key;
        private String level;
        private List<Node> children;
        private String ryid;
        private String sfzh;
        private String rwsj;
        private String twsj;
        private String lxdh;
        private String pid;
        private String mlid;
        private String choose;
        private String rymlid;
        private boolean disabled;
        private String orgcode;
        public Node() {}
        public Node(String id,String parentId,String title,String key,String level,String ryid,String choose,boolean disabled,String pid,String mlid,String rymlid,String orgcode,String sfzh,String rwsj,String twsj,String lxdh){
            super();
            this.id = id;
            this.parentId = parentId;
            this.title = title;
            this.key = key;
            this.level = level;
            this.ryid = ryid;
            this.choose = choose;
            this.disabled = disabled;
            this.pid = pid;
            this.mlid = mlid;
            this.rymlid = rymlid;
            this.orgcode = orgcode;
            this.sfzh = sfzh;
            this.rwsj = rwsj;
            this.twsj = twsj;
            this.lxdh = lxdh;
        }
        public String getsfzh(){ return  sfzh;}

        public void setsfzh(String sfzh) {
            this.sfzh = sfzh;
        }

        public String getrwsj() { return  rwsj;}

        public void setrwsj(String rwsj) {
            this.rwsj = rwsj;
        }

        public String gettwsj() { return  twsj;}

        public void settwsj(String twsj) {
            this.twsj = twsj;
        }

        public String getlxdh() { return  lxdh;}

        public void setlxdh(String lxdh) {
            this.lxdh = lxdh;
        }

        public String getorgcode() { return  orgcode;}

        public void setorgcode(String orgcode) {
            this.orgcode = orgcode;
        }

        public String getrymlid() {  return rymlid;       }

        public void setrymlid(String rymlid) {
            this.rymlid = rymlid;
        }


        public String getmlid() {
            return mlid;
        }

        public void setmlid(String mlid) {
            this.mlid = mlid;
        }

        public String getpid() {
            return pid;
        }

        public void setpid(String pid) {
            this.pid = pid;
        }

        public String getryid() {
            return ryid;
        }

        public boolean isDisabled() {
            return disabled;
        }

        public String getChoose() {
            return choose;
        }

        public void setChoose(String choose) {
            this.choose = choose;
        }

        public void setryid(String ryid) {
            this.ryid = ryid;
        }

        public void setDisabled(boolean disabled) {
            this.disabled = disabled;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getParentId() {
            return parentId;
        }

        public void setParentId(String parentId) {
            this.parentId = parentId;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }
        public String getLevel() {
            return level;
        }

        public void setLevel(String level) {
            this.level = level;
        }

        public List<Node> getChildren() {
            return children;
        }

        public void setChildren(List<Node> children) {
            this.children = children;
        }
    }
}
